<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="部门：">
                        <Select class="option" multiple :value="search.department" @on-change=" selectDem ">
                            <Option value="全部">全部</Option>
                            <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="姓名：">
                        <Input v-model="search.username" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                        <Button v-if="role < 3" style="margin-left:10px" icon="ios-download-outline" type="success" @click="exportExcel">导出Excel</Button>
                    </FormItem>

                </Form>

            </div> <!-- end search -->

            <Table :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        selectDepartment
    } from '@/libs/tools'
    import {
        exportExcel
    } from '@/api/data'
    import {
        mapGetters,
        mapMutations
    } from 'vuex'
    export default {
        name: 'daily-data-detail',
        data() {
            return {
                loading: false,

                // 搜索对象
                search: {
                    department: ['全部'],
                    start_time: '',
                    end_time: '',
                    username: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    // {
                    //     type: 'expand',
                    //     width: 50,
                    //     render: (h, params) => {
                    //         return h(tableExpand, {
                    //             props: {
                    //                 row: params.row
                    //             }
                    //         })
                    //     }
                    // },
                    {
                        title: '部门',
                        align: 'center',
                        key: 'department'
                    },
                    {
                        title: '提交人',
                        align: 'center',
                        key: 'username'
                    },
                    {
                        title: '今日到店客户',
                        align: 'center',
                        key: 'today_shop_person_num'
                    },
                    {
                        title: '定金',
                        align: 'center',
                        key: 'front_money'
                    },
                    {
                        title: '会员回款',
                        align: 'center',
                        key: 'vip_back_money',
                    },
                    {
                        title: '入驻回款',
                        align: 'center',
                        key: 'check_in_back_money'
                    },
                    {
                        title: '代办回款',
                        align: 'center',
                        key: 'agency_back_money'
                    },
                    {
                        title: '代账回款',
                        align: 'center',
                        key: 'account_back_money'
                    },
                    {
                        title: '商标回款',
                        align: 'center',
                        key: 'trademark_back_money'
                    },
                    {
                        title: '科技服务',
                        align: 'center',
                        key: 'science_money'
                    },
                    {
                        title: '其他',
                        align: 'center',
                        key: 'other_money',
                    },
                    {
                        title: '目标金额',
                        align: 'center',
                        key: 'target_money',
                    },
                    {
                        title: '目前金额',
                        align: 'center',
                        key: 'now_money',
                    },
                    {
                        title: '完成率',
                        align: 'center',
                        key: 'completion',
                    },
                    {
                        title: '日期',
                        align: 'center',
                        width: 100,
                        key: 'now_date',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 230,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'warning',
                                        size: 'default',
                                        icon: 'md-brush',
                                        disabled: this.role > 2
                                    },
                                    style: {
                                        marginRight: '14px',
                                        width: '80px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            if (this.role > 2) {
                                                return false
                                            }
                                            this.myClose('daily-data-edit')
                                            this.$_changePage('daily-data-edit', '修改每日数据', {
                                                id: params.row.id
                                            })
                                        }
                                    }
                                }, '编辑'),
                                h('Poptip', {
                                        props: {
                                            confirm: true,
                                            title: '你确定要删除吗?',
                                            placement: 'top',
                                            transfer: true
                                        },
                                        on: {
                                            'on-ok': () => {
                                                if (this.role > 1) {
                                                    return false
                                                }
                                                this.deleteByid(params.row.id)
                                            }
                                        }
                                    },
                                    [h(
                                        'Button', {
                                            props: {
                                                type: 'error',
                                                size: 'default',
                                                icon: 'ios-trash',
                                                disabled: this.role > 1
                                            },
                                            style: {
                                                marginRight: '14px',
                                                width: '80px',
                                                height: '30px'
                                            }
                                        },
                                        '删除'
                                    )]
                                )
                            ])
                        }
                    }


                ],
                data1: [],
            }
        },
        methods: {
            ...mapMutations(['myClose']),
            ...mapGetters(['getDem', 'getRole']),

            // 获取每日数据列表
            getDailyDataDetailList() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-work_record_data-ajax_get_wrd', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num
                        this.addCompletion(this.data1)

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getDailyDataDetailList()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 计算完成率
            addCompletion(data) {
                data.forEach(item => {
                    let {
                        target_money,
                        now_money
                    } = item
                    let completion = now_money > 0 ? Math.round(now_money / target_money * 100) + '%' : 0 + '%'
                    item.completion = completion
                })
            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getDailyDataDetailList()
            },

            // 选择部门
            selectDem(value) {
                this.search.department = selectDepartment(value)
            },

            // 导出
            exportExcel() {
                let {
                    ...out
                } = this.search
                out.department = JSON.stringify(out.department)
                exportExcel('admin-work_record_data-export_wrd', out)
            },

            // 删除
            deleteByid(wrdid) {
                axios.ajax_post('admin-work_record_data-ajax_del_wrd', {
                    wrdid
                }).then(res => {
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: res.data.msg,
                            duration: 3
                        })
                        this.getDailyDataDetailList()
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getDailyDataDetailList()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getDailyDataDetailList()
            },
        },
        computed: {
            demList() {
                return this.getDem()
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            this.getDailyDataDetailList()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;
            flex-wrap: wrap;

            .option {
                width: auto;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>